﻿using System;
using System.Collections.Generic;
using GxIAPINET;
using OpenCvSharp;

namespace DaHeng
{
    public class GalaxyCamera
    {
        private string Index ;
        private IGXDevice Device;
        private IGXStream DeviceStream;
        private IGXFeatureControl DeviceStreamFeatureControl;
        private IGXFeatureControl FeatureControl;
        private uint Timeout = 500;

        // todo 待添加回调采集
        public static void CaptureCallback()
        {}
        
        /// <summary>
        /// 设置相机增益
        /// </summary>
        /// <param name="value"></param>
        public void SetGain(double value)
        {
            FeatureControl.GetFloatFeature("Gain").SetValue(value);
            // FeatureControl.GetEnumFeature("GainAuto").SetValue("Off");
        }
        
        /// <summary>
        /// 设置曝光时间
        /// </summary>
        /// <param name="value"></param>
        public void SetExposure(int value)
        {
            FeatureControl.GetFloatFeature("ExposureTime").SetValue(value);
            // FeatureControl.GetEnumFeature("ExposureAuto").SetValue("Off");
        }
        
        /// <summary>
        /// 开启相机
        /// </summary>
        public void Open()
        {
            IGXFactory.GetInstance().Init();
            List<IGXDeviceInfo> listGXDeviceInfo = new List<IGXDeviceInfo>();
            IGXFactory.GetInstance().UpdateAllDeviceList(200,listGXDeviceInfo);
            Index = listGXDeviceInfo[0].GetSN();
            Device = IGXFactory.GetInstance().OpenDeviceBySN(Index,GX_ACCESS_MODE.GX_ACCESS_EXCLUSIVE);
            FeatureControl = Device.GetRemoteFeatureControl();
            UInt32 steamNum = Device.GetStreamCount();
            if (steamNum > 0)
            {
                DeviceStream = Device.OpenStream(0);
                DeviceStreamFeatureControl = DeviceStream.GetFeatureControl();
            }
            
            DeviceStream.StartGrab();
            // FeatureControl.GetIntFeature("GevHeartbeatTimeout").SetValue(300000);
            FeatureControl.GetEnumFeature("AcquisitionMode").SetValue("Continuous");
            FeatureControl.GetEnumFeature("TriggerMode").SetValue("On");
            FeatureControl.GetEnumFeature("TriggerSource").SetValue("Software");
            if(FeatureControl!=null)
                FeatureControl.GetCommandFeature("AcquisitionStart").Execute();
            
        }
        
        /// <summary>
        /// 通过软触发采集当前图像
        /// </summary>
        /// <returns>Mat 图像</returns>
        public Mat GetPicture()
        {
            if (DeviceStream != null)
            {
                DeviceStream.FlushQueue();
            }
            FeatureControl.GetCommandFeature("TriggerSoftware").Execute();
            var imageData = DeviceStream.GetImage(Timeout);
            
            // if (null != imageData)
            // {
            //     CurrentPicture.data = (imageData.GetBuffer());
            //     CurrentPicture.Height = (int)(imageData.GetHeight());
            //     CurrentPicture.Width = (int) (imageData.GetWidth());
            // }
            Mat origin_picture = new Mat((int) imageData.GetHeight(),(int) imageData.GetWidth(), MatType.CV_8UC1,(IntPtr) (imageData.GetBuffer()));
            return origin_picture;

        }

        ~GalaxyCamera()
        {
            FeatureControl.GetCommandFeature("AcquisitionStop").Execute();
            DeviceStream.StopGrab();
            DeviceStream.Close();
            Device.Close();
            IGXFactory.GetInstance().Uninit();
        }
    }

}